package com.shujia.tf

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows
import org.apache.flink.streaming.api.windowing.time.Time

object Demo6Window {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val listDS: DataStream[String] = env.socketTextStream("master", 8888)

    val wordsDS: DataStream[String] = listDS.flatMap(_.split(","))

    val kvDS: DataStream[(String, Int)] = wordsDS.map((_, 1))

    val keuByDS: KeyedStream[(String, Int), String] = kvDS.keyBy(_._1)

    /**
      * 统计最近15秒单词的数量，每隔5秒统计一次
      *
      */
    val wdinwoCountDS: DataStream[(String, Int)] = keuByDS
      //滑动窗口
      .window(SlidingProcessingTimeWindows.of(Time.seconds(15), Time.seconds(5)))
      .sum(1)

    wdinwoCountDS.print()

    env.execute()


  }

}
